package linuxbox.download.logic;

import java.util.*;

import linuxbox.download.bean.*;

public class DownLoadManager {
    public static DownLoadManager staticInstance = null;
    
    public static DownLoadManager getInstance() {
        if (staticInstance == null) {
            staticInstance = new DownLoadManager();
        }
        
        return staticInstance;
    }
    
	private DownLoadManager() {
	
	}
	
	/* DownLoad file related methods */
	
	public DownLoadObject getDownLoad(DownLoadKey key) throws Exception {
		DownLoadObject entry = null;

		DownLoadBean bean = new DownLoadBean(key);
	bean.load();
		entry = new DownLoadObject();
		entry.copyFrom(bean);

		return entry;
	}
	
	public DownLoadObject getDownLoad(int id) throws Exception {
		return getDownLoad(new DownLoadKey(id));
	}

	/*public boolean addDownLoad(DownLoadKey key) {
		try {
			DownLoadBean bean = new DownLoadBean(key);
			bean.create();
			
			if (key.getID() == 0)
			key.setID(bean.getID());
		} catch (Exception e) {
			BoxStatic.logger.debug(e);
			return false;
		}
		return true;
	}*/
	
	public DownLoadKey addDownLoad(DownLoadObject obj) throws Exception {
		DownLoadKey key = null;
		
		DownLoadBean bean = new DownLoadBean();
		bean.create(obj);
		
		key = new DownLoadKey(bean.getID());
		
		return key;
	}

	public void removeDownLoad(DownLoadKey key) throws Exception {
		DownLoadBean bean = new DownLoadBean(key);
		bean.remove();
	}

	public void removeDownLoad(int id) throws Exception {
		removeDownLoad(new DownLoadKey(id));
	}
	
	public void updateDownLoad(DownLoadObject entry) throws Exception {
		DownLoadBean bean = new DownLoadBean(new DownLoadKey(entry.getID()));
		bean.copyFrom(entry);
		bean.store();
	}
	
	protected Iterator findDownLoadBySql(String sql) throws Exception {
		Iterator it = null;

		DownLoadBean bean = new DownLoadBean(null);
		it = bean.findBySql(sql);

		return it;
	}

	public Iterator getAllDlFiles() throws Exception {
		return findDownLoadBySql(" ");
	}

	public Iterator getAllDlFiles(String sort) throws Exception {
		return findDownLoadBySql(sort);
	}

	/* DownLoad category related methods */
	
	public DlCatObject getDlCat(DlCatKey key) throws Exception {
		DlCatObject entry = null;

		DlCatBean bean = new DlCatBean(key);
		bean.load();
		entry = new DlCatObject();
		entry.copyFrom(bean);

		return entry;
	}
	
	/*public boolean addDlCat(DownLoadKey key) {
		try {
			DownLoadBean bean = new DownLoadBean(key);
			bean.create();
			
			if (key.getID() == 0)
			key.setID(bean.getID());
		} catch (Exception e) {
			BoxStatic.logger.debug(e);
			return false;
		}
		return true;
	}*/
	
	public DlCatKey addDlCat(DlCatObject obj) throws Exception {
		DlCatKey key = null;
		
		DlCatBean bean = new DlCatBean();
		bean.create(obj);
		
		key = new DlCatKey(bean.getID());

		return key;
	}

	public void removeDlCat(DlCatKey key) throws Exception {
		DlCatBean bean = new DlCatBean(key);
		bean.remove();
	}
	
	public void removeDlCat(int id) throws Exception {
		removeDlCat(new DlCatKey(id));
	}

	public void updateDlCat(DlCatObject entry) throws Exception {
		DlCatBean bean = new DlCatBean(new DlCatKey(entry.getID()));
		bean.copyFrom(entry);
		bean.store();
	}
	
	protected Iterator findDlCatBySql(String sql) throws Exception {
		Iterator it = null;

		DlCatBean bean = new DlCatBean(null);
		it = bean.findBySql(sql);
		
		return it;
	}

	public Iterator getAllDlCats() throws Exception {
		return findDlCatBySql(" ");
	}
}
